package com.wy.controller.sys;

import com.wy.constants.CommonValues;
import com.wy.model.sys.SystemValues;
import com.wy.service.sys.SystemValuesService;
import com.wy.utils.BaseResponseInfo;
import com.wy.utils.ConstantData;
import com.wy.utils.HTTPStatus;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/values")
public class SystemValuesController {
    private Logger log = LoggerFactory.getLogger(MenuController.class);

    @Resource(name = "systemValuesServiceImpl")
    private SystemValuesService systemValuesService;

    @Value("${location.province}")
    private String province;

    @Value("${location.city}")
    private String city;

    @Value("${location.region}")
    private String region;

    /**
     * 增加数据字典记录
     * @param values
     * @return
     */
    @PostMapping("")
    public BaseResponseInfo saveValue(@RequestBody SystemValues values){
        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
        Map<String, Object> data = new HashMap<String, Object>();
        try{
            if(systemValuesService.selectByNameAndType(values) == null){
                systemValuesService.insert(values);
                baseResponseInfo.code = HTTPStatus.OK;
                data.put("message", ConstantData.MSG_SUCCESS_INFO);
            }else{
                baseResponseInfo.code = HTTPStatus.BAD_REQUEST;//已经存在记录
            }

        }catch (Exception e){
            e.printStackTrace();
            baseResponseInfo.code = HTTPStatus.INTERNAL_SERVER_ERROR;
        }
        log.debug("添加完成");
        baseResponseInfo.data = data;
        return baseResponseInfo;
    }

    /**
     * 更新数据字典
     * @param values
     * @return
     */
    @PutMapping("")
    public BaseResponseInfo updateValue(@RequestBody  SystemValues values){
        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
        Map<String, Object> data = new HashMap<String, Object>();
        systemValuesService.update(values);
        data.put("message","修改完成");
        baseResponseInfo.data = data;
        return baseResponseInfo;
    }

    /**
     * 根据id删除数据字典项
     * @param groupId
     * @return
     */
    @DeleteMapping("")
    public  BaseResponseInfo delById(@RequestBody List<String> groupId){
        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
        Map<String, Object> data = new HashMap<String, Object>();
        try{
            systemValuesService.delete(groupId);
            baseResponseInfo.code = HTTPStatus.OK;
            data.put("message", ConstantData.MSG_SUCCESS_INFO);
        }catch (Exception e){
            baseResponseInfo.code = HTTPStatus.INTERNAL_SERVER_ERROR;
        }
        baseResponseInfo.data = data;
        return baseResponseInfo;
    }

    /**
     * 根据类型查询数据字典
     * @param type
     * @return
     */
    @GetMapping("/{type}")
    public BaseResponseInfo getValuesByType(@PathVariable String type, HttpServletRequest request,String path){
       BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
       Map<String, Object> data = new HashMap<String, Object>();
        try{
           baseResponseInfo.code = HTTPStatus.OK;
           data.put("data", systemValuesService.listByType(type,path,null));
       }catch (Exception e){
           e.printStackTrace();
           baseResponseInfo.code = HTTPStatus.INTERNAL_SERVER_ERROR;
       }
       baseResponseInfo.data = data;
       return baseResponseInfo;
   }

   @GetMapping("/list")
   public BaseResponseInfo list(){
       BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
       Map<String, Object> data = new HashMap<String, Object>();
       List<SystemValues> valuesList = new ArrayList<SystemValues>();
       try{
           valuesList = systemValuesService.listAll();
           baseResponseInfo.code = HTTPStatus.OK;
           data.put("message", valuesList);
       }catch (Exception e){
           e.printStackTrace();
           baseResponseInfo.code = HTTPStatus.INTERNAL_SERVER_ERROR;
       }finally {
           valuesList = null;
       }
       baseResponseInfo.data = data;
       return  baseResponseInfo;
   }

    /**
     * 获取配置文件中当前部署的城市
     * @return
     */
   @GetMapping("/location")
   public BaseResponseInfo getLocation(){
       BaseResponseInfo baseResponseInfo = new BaseResponseInfo();

        String[] location = new String[3];
        location[0] = province;
        location[1] = city;
        location[2] = region;

       baseResponseInfo.data = location;
       return baseResponseInfo;

   }

}
